﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyPiano
{
    public partial class WaitingForm : Form
    {        
        public int progress;
        int maxValue = 100;
        Action<WaitingForm> process;
        Control control;

        public WaitingForm()
        {
            InitializeComponent();
        }

        int GetPercentage()
        {
            return pbb_main.Value * 100 / pbb_main.Maximum;
        }

        public int Value
        {
            set
            {
                if (value != maxValue)
                {                    
                    if (value > 0)
                    {
                        pbb_main.Value = value;
                        pbb_main.Value = value - 1;
                        pbb_main.Value = value;                        
                    }                    
                }
                else
                {
                    pbb_main.Maximum = value + 1;
                    pbb_main.Value = value + 1;
                    pbb_main.Maximum = value;
                    pbb_main.Value = value;
                }
                lb_pg.Text = GetPercentage() + " %";
                this.Refresh();
            }
        }

        void SetProcess(Action<WaitingForm> process)
        {
            this.process = process;
        }
        void SetMaxValue(int maxV)
        {
            this.maxValue = maxV;
            pbb_main.Maximum = maxV;
        }

        void Processing()
        {
            Thread thread = new Thread(() => {
                control.Invoke(process, this);
            });

            thread.Start();
        }

        void UpdateProgress()
        {
            while (this.progress < maxValue)
            {
                this.Value = progress;                
            }
            this.Value = maxValue;            
        }

        public void Create(string title,int maxV,Action<WaitingForm> process,Control control)
        {
            this.Text = title;
            this.SetMaxValue(maxV);
            this.SetProcess(process);
            this.control = control;
        }

        public void Begin()
        {
            Thread thread = new Thread(() => {
                //开启窗口
                this.Show();
                //在另一线程处理
                this.Processing();
                //在本线程更新
                this.UpdateProgress();
                //关闭窗口
                this.Close();
                this.Dispose();
            });
            thread.Start();
        }

        public static void ShowForm(string title, int maxV, Action<WaitingForm> process, Control control)
        {
            WaitingForm form = new WaitingForm();

            if (maxV > 50)
            {
                form.Create(title, maxV, process, control);
                form.Begin();
            }
            else
            {
                process.Invoke(null);
                form.Dispose();
            }
        }
    }
}
